/**
 * 
 */
package org.agiba.jira.plugin.model;

import java.util.ArrayList;
import java.util.List;

/**
 * Model class to persist a XWiki instance
 * 
 * @author kamtho
 * 
 */
public class XWikiInstance {
	private String key;
	private String alias;
	private String url;
	private XWikiCredentials credentials = XWikiCredentials.EMPTY;
	private List<XWikiSpaceInfo> spaces = new ArrayList<XWikiSpaceInfo>();
	private boolean active = true;

	public static XWikiInstance EMPTY = new XWikiInstance().withAlias("")
			.withUrl("").withKey("")
			.withCredentials(new XWikiCredentials().withUid("").withPwd(""));

	/**
	 * @return the alias
	 */
	public String getAlias() {
		return alias;
	}

	/**
	 * @param alias
	 *            the alias to set
	 */
	public void setAlias(String alias) {
		this.alias = alias;
	}

	public XWikiInstance withAlias(String alias) {
		setAlias(alias);
		return this;
	}

	/**
	 * @return the url
	 */
	public String getUrl() {
		return url;
	}

	/**
	 * @param url
	 *            the url to set
	 */
	public void setUrl(String url) {
		this.url = url;
	}

	public XWikiInstance withUrl(String url) {
		setUrl(url);
		return this;
	}

	/**
	 * @return the active
	 */
	public boolean isActive() {
		return active;
	}

	/**
	 * @param active
	 *            the active to set
	 */
	public void setActive(boolean active) {
		this.active = active;
	}

	/**
	 * @return the credentials
	 */
	public XWikiCredentials getCredentials() {
		return credentials;
	}

	/**
	 * @param credentials
	 *            the credentials to set
	 */
	public void setCredentials(XWikiCredentials credentials) {
		this.credentials = credentials;
	}

	public XWikiInstance withCredentials(XWikiCredentials credentials) {
		setCredentials(credentials);
		return this;
	}

	/**
	 * @return the key
	 */
	public String getKey() {
		return key;
	}

	/**
	 * @param key
	 *            the key to set
	 */
	public void setKey(String key) {
		this.key = key;
	}
	
	public XWikiInstance withKey(String key){
		setKey(key);
		return this;
	}

	/**
	 * @return the spaces
	 */
	public List<XWikiSpaceInfo> getSpaces() {
		return spaces;
	}

	public List<String> getSpacesAsStringList() {
		List<String> result = new ArrayList<String>();
		for (XWikiSpaceInfo spaceInfo : spaces) {
			result.add(spaceInfo.getKey());
		}
		return result;
	}

	/**
	 * @param spaces
	 *            the spaces to set
	 */
	public void setSpaces(List<XWikiSpaceInfo> spaces) {
		this.spaces = spaces;
	}

	public XWikiInstance withSpaces(List<XWikiSpaceInfo> spaces) {
		setSpaces(spaces);
		return this;
	}

	public String getUniqueKeyForSpace(String space) {
		return getKey() + ":" + space;
	}

}
